package org.apache.lucene.demo;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.CharArrayReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringBufferInputStream;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;

//读取天涯的文件的方法
public class FileTianyaDocument
{
	//从文件中创建Document
	public static Document Document(File f) throws 
		IOException
	{
		Document doc = new Document();
		
		//存储文件的地址
		doc.add(new Field("path", f.getPath(), Field.Store.YES, 
				Field.Index.NOT_ANALYZED));
		
		//获取文件的内容至String
		String content = FileDocument.FileToString(f);
		UserCharArrayReader charReader = new UserCharArrayReader(content.toCharArray());
		
		//保存文件的四个域
		String title;
		String question;
		String classification;
		String answer;
		String line;
		
		/*
		boolean titleAppeared = false;
		boolean questionAppeared = false;
		boolean classificationAppeared = false;
		boolean answerAppeared = false;
		boolean lineAppeared = false;
		*/
		
		//开始转化文件至Fields
		title = GetFieldContent(charReader, "title");
		classification = GetFieldContent(charReader, "classify");
		question = GetFieldContent(charReader, "question");
		answer = GetFieldContent(charReader, "answer");
		
		doc.add(new Field("title", title, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS));
		doc.add(new Field("question", question, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS));
		doc.add(new Field("classify", classification, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS));
		doc.add(new Field("answer", answer, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS));
		
		return doc;
		
	}
	
	private static String GetFieldContent(UserCharArrayReader reader, String field) throws IOException
	{
		int appearedCount = 0;
		String ret = new String("");
		Pattern pattern;
		
		if (field.compareTo("answer") == 0)
		{
			pattern = Pattern.compile("^<(.)?" + field + "(\\d)+>$");
			String line;
			while ((line = reader.readLine()) != null)
			{
				if (line != null)
				{
					Matcher matcher = pattern.matcher(line);
					if (matcher.find())
					{
						appearedCount++;
						continue;
					}
					if (appearedCount > 0)
					{
						ret += line;
					}
					
				}
				else
				{
					break;
				}
			}
			
			return ret;
		}
		else
		{
			pattern = Pattern.compile("^<(.)?" + field + ">$");
			
			//出现了两次才算一个完整的Field
			while (appearedCount < 2)
			{
				String line = reader.readLine();
				if (line != null)
				{
					Matcher matcher = pattern.matcher(line);
					if (matcher.find())
					{
						appearedCount++;
						continue;
					}
					if (appearedCount > 0)
					{
						ret += line;
					}
				}
				else
				{
					break;
				}
			}
			return ret;
		}
	}
	
	public static void main(String[] args) throws IOException
	{
		Document tianyaDoc = FileTianyaDocument.Document(
				new File("D:\\corpus\\tianya_processed\\0a6fdbbdd607f449.txt"));
	}
}